﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GUI.TraCuu_Update
{
    public partial class GVPagesGUI : UserControl
    {
        public class GVPagesItem
        {
            bool _edited;
            List<String> _data;

            public GVPagesItem()
            {
                _edited = false;
                _data = null;
            }

            public bool Edited
            {
                get { return _edited; }
                set { _edited = value; }
            }

            public List<String> Data
            {
                get { return _data; }
                set { _data = value; }
            }
        }

        public class GVPagesColumn
        {
            DataGridViewColumn _Column;
            Action<object, KeyPressEventArgs> _ValidKeyCallback;

            public GVPagesColumn(DataGridViewColumn cl, Action<object, KeyPressEventArgs> cb)
            {
                _Column = cl;
                _ValidKeyCallback = cb;
            }

            public System.Windows.Forms.DataGridViewColumn Column
            {
                get { return _Column; }
                set { _Column = value; }
            }

            public Action<object, KeyPressEventArgs> ValidKeyCallback
            {
                get { return _ValidKeyCallback; }
                set { _ValidKeyCallback = value; }
            }
        }

        GUI.TraCuu_Update.GVPageInf _Parrent;
        int _pages;
        List<GVPagesItem> _Items;
        Action<List<String>> _Callback; // get selected item
        List<Action<object, KeyPressEventArgs>> _ValidKey; // validate key press

        public GVPagesGUI(
            GUI.TraCuu_Update.GVPageInf parrent,
            //List<DataGridViewColumn> colums,
            List<GVPagesColumn> colums,
            Action<List<String>> callback)
        {
            InitializeComponent();

            _Parrent = parrent;
            _Callback = callback;
            _ValidKey = new List<Action<object, KeyPressEventArgs>>();

            gv_List.Columns.Clear();
            DataGridViewTextBoxCell cell = new DataGridViewTextBoxCell();

            DataGridViewColumn clSTT =
               new DataGridViewColumn();
            clSTT.Name = "STT";
            clSTT.HeaderText = "STT";
            clSTT.Visible = true;
            clSTT.ReadOnly = true;
            clSTT.CellTemplate = cell;
            gv_List.Columns.Add(clSTT);
            _ValidKey.Add(null);

            foreach (GVPagesColumn cl in colums)
            {
                cl.Column.CellTemplate = cell;
                if (cl.Column.ReadOnly == false)
                    btn_CapNhat.Visible = true;
                gv_List.Columns.Add(cl.Column);
                _ValidKey.Add(cl.ValidKeyCallback);
            }

            _pages = 0;
            _Items = null;
        }

        public void Reload()
        {
            UpdateCB();
            if (_pages > 0)
                cbx_Page.SelectedIndex = 0;
            else
                gv_List.Rows.Clear();
        }

        private void GVPagesGUI_Load(object sender, EventArgs e)
        {
            Reload();
        }

        private void UpdateCB()
        {
            cbx_Page.Items.Clear();
            _pages = _Parrent.getPages();
            if (_pages % 10 != 0)
                _pages = (_pages / 10) + 1;
            else
                _pages = _pages / 10;

            String[] range = new String[_pages];
            for (int i = 0; i < _pages; i++)
                range[i] = (i + 1).ToString() + "/" + _pages.ToString();
            cbx_Page.Items.AddRange(range);
        }

        private void UpdateGV()
        {
            gv_List.Rows.Clear();
            _Items = _Parrent.
                getItemsPerPage(cbx_Page.SelectedIndex+1);
            foreach (GVPagesItem item in _Items)
            {
                int i = 0;
                List<String> data = item.Data;
                String[] zz = new String[data.Count + 1];
                zz[i++] = ((cbx_Page.SelectedIndex * 10) + 
                    (gv_List.Rows.Count + 1)).ToString(); // STT
                foreach (String s in data)
                    zz[i++] = s;
                gv_List.Rows.Add(zz);
            }
        }

        private void cbx_Page_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (_Items != null)
                _Parrent.processItemChange(_Items);
            UpdateGV();
        }

        private void btn_Back_Click(object sender, EventArgs e)
        {
            if (cbx_Page.SelectedIndex - 1 >= 0)
                cbx_Page.SelectedIndex -= 1;
        }

        private void btn_Next_Click(object sender, EventArgs e)
        {
            if (cbx_Page.SelectedIndex + 1 < _pages)
                cbx_Page.SelectedIndex += 1;
        }

        private void gv_List_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            _Items[gv_List.CurrentCell.RowIndex].Edited = true;
            _Items[gv_List.CurrentCell.RowIndex].Data[gv_List.CurrentCell.ColumnIndex - 1] =
                gv_List.CurrentCell.Value.ToString();
        }

        private void btn_CapNhat_Click(object sender, EventArgs e)
        {
            if (_Items != null)
                _Parrent.processItemChange(_Items);
        }

        private void gv_List_SelectionChanged(object sender, EventArgs e)
        {
            if (_Callback != null)
                _Callback(_Items[gv_List.CurrentCell.RowIndex].Data);
        }

        private void gv_List_EditingControlShowing(object sender, 
            DataGridViewEditingControlShowingEventArgs e)
        {
            int clidx = (sender as DataGridView).CurrentCell.ColumnIndex;
            if (_ValidKey[clidx] != null)
            {
                e.Control.KeyPress -= new KeyPressEventHandler(_ValidKey[clidx]);
                e.Control.KeyPress += new KeyPressEventHandler(_ValidKey[clidx]);
            }
        }
    }
}
